<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">简历管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>简历管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="user-search-key">
                <option value="">-请选择-</option>
                <option value="id">Id</option>
                <option value="userId">拥有者</option>
                <option value="name">简历名称</option>
            </select>&emsp;
            <input id="user-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" placeholder="请选择开始时间" class="layui-input" id="start">
            </div>
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" placeholder="请选择结束时间" class="layui-input" id="end">
            </div>
            <button id="resume-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="resume-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
            <button id="resume-btn-export" class="layui-btn icon-btn"><i class="layui-icon">&#xe601;</i>导出</button>
        </div>

        <table class="layui-table" id="resume-table" lay-filter="resume-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="resume-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="delete">删除</a>
<!--    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>-->
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin','laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
            ,type:'datetime'
            ,format:'yyyy-MM-dd HH:mm:ss'
        });
        laydate.render({
            elem: '#end' //指定元素
            ,type:'datetime'
            ,format:'yyyy-MM-dd HH:mm:ss'
        });
        //if( admin.hasPerm('user:add') ) {
        //   $('#btnAdd').hide();
        //}

        // 添加按钮绑定权限
        if(!admin.hasPerm('resume:saveOrUpdate')) {
            $('#resume-btn-add').hide();
        }

        // 导出按钮绑定权限
        if(!admin.hasPerm('resume:saveOrUpdate')) {
            $('#resume-btn-export').hide();
        }



        // 渲染表格
        var tableIns=table.render({
            elem: '#resume-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-user/resumes',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token
            },
            response:{
                statusName: 'code',
                statusCode:200
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: '简历ID'},
                {field: 'addresser.username', sort: true, title: '上传人',templet:(data)=>{
                        return data.addresser==null?'':data.addresser.username
                    }},
                {field: 'name', sort: true, title: '简历名称'},
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {
                    sort: true, templet: function (d) {
                        return util.toDateString(d.updateTime);
                    }, title: '修改时间'
                },
                {align: 'center', toolbar: '#resume-table-bar', title: '操作',width: 150}
            ]]
        });

        // 添加按钮点击事件
        $('#resume-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(resume-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
            else if (layEvent === 'delete') { // 重置密码
                layer.confirm('确定删除吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-user/resume/'+obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            tableIns()
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }

                    }, 'DELETE');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_resume', data);
            var title = data ? '修改简历' : '添加简历';
            admin.popupCenter({
                title: title,
                path: 'pages/system/resume_form.html',
                finish: function () {
                    table.reload('resume-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#resume-btn-search').click(function () {
            var key = $('#user-search-key').val();
            var value = $('#user-search-value').val();
            table.reload('resume-table', {where: {searchKey: key, searchValue: value,end:$("#end").val(),start:$("#start").val()}});
        });


        // 导出
        $('#resume-btn-export').click(function () {
            console.log("导出")
            // var xhr = new XMLHttpRequest();
            // xhr.open('POST', url, true);
            // xhr.responseType = "blob";
            // xhr.setRequestHeader("client_type", "DESKTOP_WEB");
            // xhr.onload = function() {
            //     if (this.status == 200) {
            //         var blob = this.response;
            //         var objecturl = URL.createObjectURL(blob);
            //         window.location.href = objecturl;
            //     }
            // }
            // xhr.send();
            var key = $('#user-search-key').val();
            var value = $('#user-search-value').val();
            admin.req('api-user/resumes'
                ,{searchKey: key, searchValue: value,end:$("#end").val(),start:$("#start").val(),page:0,limit:1000}
                ,(res)=>{
                    if(res.code===200){
                        table.exportFile(tableIns.config.id,res.data,'xls');
                    }else {
                        layer.msg(res.msg)
                    }

                }
                ,'GET')
        });

    });
</script>